#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <gtkmm.h>
#include <vector>
#include <string>
#include <deque>
#include "processtable.h"
#include "processmanager.h"
#include "systemmonitor.h"

// 应用历史记录列模型
class AppHistoryColumns : public Gtk::TreeModel::ColumnRecord {
public:
    AppHistoryColumns() {
        add(m_col_app_name);
        add(m_col_first_run);
        add(m_col_last_run);
        add(m_col_run_count);
    }

    Gtk::TreeModelColumn<Glib::ustring> m_col_app_name;
    Gtk::TreeModelColumn<Glib::ustring> m_col_first_run;
    Gtk::TreeModelColumn<Glib::ustring> m_col_last_run;
    Gtk::TreeModelColumn<int> m_col_run_count;
};

// 启动项列模型
class StartupColumns : public Gtk::TreeModel::ColumnRecord {
public:
    StartupColumns() {
        add(m_col_startup_name);
        add(m_col_startup_command);
        add(m_col_startup_status);
    }

    Gtk::TreeModelColumn<Glib::ustring> m_col_startup_name;
    Gtk::TreeModelColumn<Glib::ustring> m_col_startup_command;
    Gtk::TreeModelColumn<Glib::ustring> m_col_startup_status;
};

// 工具箱列模型
class UsersColumns : public Gtk::TreeModel::ColumnRecord {
public:
    UsersColumns() {
        add(m_col_username);
        add(m_col_uid);
        add(m_col_gid);
        add(m_col_home_dir);
        add(m_col_shell);
    }

    Gtk::TreeModelColumn<Glib::ustring> m_col_username;
    Gtk::TreeModelColumn<Glib::ustring> m_col_uid;
    Gtk::TreeModelColumn<Glib::ustring> m_col_gid;
    Gtk::TreeModelColumn<Glib::ustring> m_col_home_dir;
    Gtk::TreeModelColumn<Glib::ustring> m_col_shell;
};

// AI管家列模型
class ServicesColumns : public Gtk::TreeModel::ColumnRecord {
public:
    ServicesColumns() {
        add(m_col_service_name);
        add(m_col_service_status);
        add(m_col_service_description);
    }

    Gtk::TreeModelColumn<Glib::ustring> m_col_service_name;
    Gtk::TreeModelColumn<Glib::ustring> m_col_service_status;
    Gtk::TreeModelColumn<Glib::ustring> m_col_service_description;
};

class MainWindow : public Gtk::Window {
public:
    MainWindow();
    virtual ~MainWindow();

protected:
    // 信号处理
    void on_refresh_button_clicked();
    void on_end_task_button_clicked();
    void on_new_task_button_clicked();
    void on_process_filter_changed();
    void on_tab_changed();
    bool on_system_info_update();
    
    // 绘图处理
    bool on_cpu_draw(const Cairo::RefPtr<Cairo::Context>& cr);
    bool on_memory_draw(const Cairo::RefPtr<Cairo::Context>& cr);
    bool on_disk_draw(const Cairo::RefPtr<Cairo::Context>& cr);
    
    // 性能概览页面处理
    void on_cpu_button_clicked();
    void on_memory_button_clicked();
    void on_disk_button_clicked();
    void on_network_button_clicked();  // 添加网络按钮点击处理函数
    
    // 定时器回调函数
    bool on_process_timer_callback();
    bool on_system_timer_callback();
    
    // 成员变量
    // 主布局
    Gtk::Box m_main_box;
    Gtk::Box m_button_box;
    
    // 标签页
    Gtk::Notebook m_notebook;
    
    // 进程标签页
    Gtk::Box m_process_box;
    Gtk::Box m_filter_box;
    
    // 过滤按钮
    Gtk::RadioButton m_all_processes_radio;
    Gtk::RadioButton m_user_processes_radio;
    Gtk::RadioButton m_system_processes_radio;
    Gtk::RadioButton m_services_radio;
    
    // 进程表格
    ProcessTable m_process_table;
    
    // 性能标签页 - 概览页面
    Gtk::Box m_performance_box;
    Gtk::Box m_perf_summary_box;
    Gtk::Box m_perf_detail_box;
    Gtk::Button m_cpu_button;
    Gtk::Button m_memory_button;
    Gtk::Button m_disk_button;
    Gtk::Button m_network_button;  // 添加网络按钮
    Gtk::Label m_cpu_label;
    Gtk::Label m_memory_label;
    Gtk::Label m_disk_label;
    
    // 系统信息显示
    Gtk::Box m_system_info_box;
    Gtk::Label m_system_info_title;
    Gtk::Grid m_system_info_grid;
    Gtk::Label m_os_label_title;
    Gtk::Label m_os_label_value;
    Gtk::Label m_kernel_label_title;
    Gtk::Label m_kernel_label_value;
    Gtk::Label m_hostname_label_title;
    Gtk::Label m_hostname_label_value;
    Gtk::Label m_uptime_label_title;
    Gtk::Label m_uptime_label_value;
    Gtk::Label m_load_label_title;
    Gtk::Label m_load_label_value;
    Gtk::Label m_processes_label_title;
    Gtk::Label m_processes_label_value;
    Gtk::Label m_cpu_model_label_title;
    Gtk::Label m_cpu_model_label_value;
    Gtk::Label m_cpu_cores_label_title;
    Gtk::Label m_cpu_cores_label_value;
    Gtk::Label m_network_label_title;
    Gtk::Label m_network_label_value;
    
    // 添加发行版标签
    Gtk::Label m_distro_label_title;
    Gtk::Label m_distro_label_value;
    
    // CPU详细页面
    Gtk::Box m_cpu_detail_box;
    Gtk::Box m_cpu_graph_box;
    Gtk::Frame m_cpu_frame;
    Gtk::DrawingArea m_cpu_drawing_area;
    std::deque<double> m_cpu_history;
    Gtk::Box m_cpu_info_box;
    Gtk::Label m_cpu_info_label;
    Gtk::Button m_cpu_back_button;
    
    // 内存详细页面
    Gtk::Box m_memory_detail_box;
    Gtk::Box m_memory_graph_box;
    Gtk::Frame m_memory_frame;
    Gtk::DrawingArea m_memory_drawing_area;
    std::deque<double> m_memory_history;
    Gtk::Box m_memory_info_box;
    Gtk::Label m_memory_info_label;
    Gtk::Button m_memory_back_button;
    
    // 磁盘详细页面
    Gtk::Box m_disk_detail_box;
    Gtk::Box m_disk_graph_box;
    Gtk::Frame m_disk_frame;
    Gtk::DrawingArea m_disk_drawing_area;
    std::deque<double> m_disk_history;
    Gtk::Box m_disk_info_box;
    Gtk::Label m_disk_info_label;
    Gtk::Button m_disk_back_button;
    
    // 应用历史记录标签页
    Gtk::Box m_app_history_box;
    Glib::RefPtr<Gtk::ListStore> m_app_history_liststore;
    AppHistoryColumns m_app_history_columns;
    
    // 启动标签页
    Gtk::Box m_startup_box;
    Glib::RefPtr<Gtk::ListStore> m_startup_liststore;
    StartupColumns m_startup_columns;
    
    // 工具箱标签页
    Gtk::Box m_toolbox_box;
    Glib::RefPtr<Gtk::ListStore> m_toolbox_liststore;
    UsersColumns m_toolbox_columns;
    
    // AI管家标签页
    Gtk::Box m_ai_master_box;
    Glib::RefPtr<Gtk::ListStore> m_ai_master_liststore;
    ServicesColumns m_ai_master_columns;
    
    // 按钮
    Gtk::Button m_refresh_button;
    Gtk::Button m_end_task_button;
    Gtk::Button m_new_task_button;
    
    // 管理器
    ProcessManager m_process_manager;
    SystemMonitor m_system_monitor;
    
    // 定时器
    sigc::connection m_process_timer;
    sigc::connection m_system_timer;
    
    // 记忆选中的进程PID
    int m_last_selected_pid;
    
    // 记忆选中的进程过滤器
    int m_last_selected_filter;
};

#endif // MAINWINDOW_H